/**
 * 认证相关API服务
 */

import api, { getAuthHeaders } from './api';

export interface RegisterRequest {
  username: string;
  email: string;
  password: string;
  nickname: string;
}

export interface LoginRequest {
  usernameOrEmail: string;
  password: string;
}

export interface User {
  id: number;
  username: string;
  email: string;
  nickname?: string;
  avatar_url?: string;
  created_at: string;
}

export interface AuthResponse {
  access_token: string;
  user: User;
}

export class AuthService {
  /**
   * 用户注册
   */
  static async register(data: RegisterRequest): Promise<AuthResponse> {
    const response = await fetch(`${api.baseURL}/auth/register`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '注册失败');
    }

    return response.json();
  }

  /**
   * 用户登录
   */
  static async login(data: LoginRequest): Promise<AuthResponse> {
    const response = await fetch(`${api.baseURL}/auth/login`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '登录失败');
    }

    return response.json();
  }

  /**
   * 获取用户信息
   */
  static async getProfile(): Promise<User> {
    const response = await fetch(`${api.baseURL}/auth/profile`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        ...getAuthHeaders(),
      },
    });

    if (!response.ok) {
      throw new Error('获取用户信息失败');
    }

    return response.json();
  }

  /**
   * 更新用户信息
   */
  static async updateProfile(data: { nickname?: string; avatar_url?: string }): Promise<User> {
    const response = await fetch(`${api.baseURL}/users/profile`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        ...getAuthHeaders(),
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '更新用户信息失败');
    }

    const result = await response.json();
    return result.user;
  }

  /**
   * 检查用户名是否存在
   */
  static async checkUsername(username: string): Promise<boolean> {
    const response = await fetch(`${api.baseURL}/users/check-username?username=${username}`, {
      method: 'GET',
    });

    if (!response.ok) {
      throw new Error('检查用户名失败');
    }

    const result = await response.json();
    return result.exists;
  }

  /**
   * 检查邮箱是否存在
   */
  static async checkEmail(email: string): Promise<boolean> {
    const response = await fetch(`${api.baseURL}/users/check-email?email=${email}`, {
      method: 'GET',
    });

    if (!response.ok) {
      throw new Error('检查邮箱失败');
    }

    const result = await response.json();
    return result.exists;
  }
}
